import os
import pandas as pd
import os
import sys

FILE_ABS_PATH = os.path.dirname(__file__)
ROOT_PATH = os.path.join(FILE_ABS_PATH, os.pardir)
sys.path.append(ROOT_PATH)
from script_convert_rating import filter_unused_data


def convert_ratings(input_fname, output_fname):
    n_test = 10
    n_train = 10
    rating = pd.DataFrame([], columns=['userID', 'itemID', 'rating'])

    # for i in range(n_test):
    #     read_dir = os.path.join(input_fname, 'test_{}.txt'.format(i))
    #     # read_dir = os.path.join(input_fname, 'test_process.txt'.format(i))
    #     df = pd.read_csv(read_dir, sep='\t', index_col=None, header=None)
    #     df.columns = ['userID', 'itemID', 'rating']
    #     rating = pd.concat([rating, df])

    n_train = 4
    for i in range(n_train):
        read_dir = os.path.join(input_fname, 'train_{}.txt'.format(i))
        # read_dir = os.path.join(input_fname, 'test_process.txt'.format(i))
        df = pd.read_csv(read_dir, sep='\t', index_col=None, header=None)
        df.columns = ['userID', 'itemID', 'rating']
        rating = pd.concat([rating, df])
        print(rating)

    rating = filter_unused_data.convert_ratings(rating)
    filter_unused_data.stats_rating(rating)

    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))


if __name__ == '__main__':
    basic_dir = '/home/bianzheng/Dataset/Recommendation/yahoomusic/ydata-ymusic-user-song-ratings-meta-v1_0'
    output_fname = '/home/bianzheng/rec2-mips/intermediate-rating-csv/yahoomusic_big.csv'
    # print(f'./yahoomusicbig {basic_dir} {output_fname}')

    convert_ratings(input_fname=basic_dir,
                    output_fname='/home/bianzheng/rec2-mips/intermediate-rating-csv/yahoomusic_big.csv')
